﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using FSS.Core.Util;
using FSS.Core.Controls;
using FSS.BLL.Entities;
using FSS.BLL.Services;
using FSS.BLL;

namespace SalesManagement.Modules.Purchase
{
    public partial class VoucherInList : BasePage
    {
        public VoucherInList()
            : base(SCREENS.VOUCHERINLIST)
        {

        }
        protected void Page_Load(object sender, EventArgs e)
        {
           //string tr=  ResourceManager2.DirectMethodNamespace.ToString();
        }
        /// <summary>
        /// Khi load page success
        /// </summary>
        [DirectMethod]
        public void DocumentReady()
        {
            BindingProvider();
            BindingMaturity();        
        }
        protected void VoucherList_OnRefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            int start = int.Parse(e.Parameters["start"]);
            int limit = int.Parse(e.Parameters["limit"]);
            limit += start;
             
            VoucherEntity Parramter = new VoucherEntity();
            if (!string.IsNullOrEmpty(txtVoucherNo.Text))
                Parramter.VoucherNo = txtVoucherNo.Text;
            if (dfDateFrom.SelectedValue != null)
                Parramter.FromDate = dfDateFrom.SelectedDate;
            if (dfDateTo.SelectedValue != null)
                Parramter.ToDate= dfDateTo.SelectedDate;
            int ProviderID = Format.ConvertToInt(cbProvider.SelectedItem.Value);
            if (ProviderID > 0)
                Parramter.ObjectId = ProviderID;
            int MaturityID = Format.ConvertToInt(cbMaturity.SelectedItem.Value);
            if (MaturityID > 0)
                Parramter.MaturityId = MaturityID;
            VoucherService Service = VoucherService.GetInstance();
            Parramter.Start = start;
            Parramter.EndNum = limit;
            Parramter.TotalRow = 0;
            Parramter.BranchId = FSSContext.Current.BranchId;
            IList<VoucherEntity> Resul = Service.SearData(Parramter);
            e.Total = Parramter.TotalRow;
            stVoucherList.DataSource = Resul;
            stVoucherList.DataBind();
        }
        /// <summary>
        /// Tìm kiếm phiếu nhập
        /// </summary>
        [DirectMethod]
        public void SearchDocument(int Page)
        {
           

        }
        [DirectMethod]
        public bool DeleteProcess(Int64 VoucherID)
        {
            VoucherService service = VoucherService.GetInstance();
            bool resul = service.Delete(VoucherID);
            return resul;
        }
        [DirectMethod]
        public void SelectionChange(int MasterID)
        {
            VoucherDetailService service = VoucherDetailService.GetInstance();        
            IList<VoucherDetailEntity> resul = service.GetAllDetailByMasterID(MasterID);
            if (resul != null)
                stVoucherDetail.DataSource = resul;
             
            stVoucherDetail.DataBind();
        }
        #region binding data
        /// <summary>
        /// Gan nha cung cap
        /// </summary>
        private void BindingProvider()
        {
            ObjectService service = ObjectService.GetInstance();
            IList<ObjectEntity> Resul = service.GetAllProvider();
            Resul.Insert(0,new ObjectEntity
            {
                ObjectId = -1,
                ObjectName = "Tất cả"
            });
                stProvider.DataSource = Resul;
            stProvider.DataBind();
            cbProvider.SelectedIndex = 0;
        }
        /// <summary>
        /// Binding Han thanh toán
        /// </summary>
        private void BindingMaturity()
        {
            MaturityService service = MaturityService.GetInstance();
            IList<MaturityEntity> Resul = service.LoadAll();
            Resul.Insert(0, new MaturityEntity
            {
                MaturityId = -1,
                MaturityName = "Tất cả"
            });
            stMaturity.DataSource = Resul;
            stMaturity.DataBind();
            cbMaturity.SelectedIndex = 0;
        }

        #endregion

    }
}
